/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.adcc.webapp.action;

import java.io.Serializable;
import java.util.Date;
import vn.adcc.model.Customer;
import vn.adcc.service.CustomerManager;

/**
 *
 * @author ducanh.nguyen
 */
public class CustomerForm extends BasePage implements Serializable {

    private CustomerManager customerManager;
    private String id;
    private Customer customer = new Customer();

    public String add() {
        customer = new Customer();
        return "customerForm";
    }

    public String edit() {
        customer = customerManager.get(Integer.valueOf(id));
        return "customerForm";
    }

    public String save() {
        try {
            if (customer.getId() == null) {
                customer.setDateCreated(new Date());
            }
            customerManager.save(customer);

        } catch (Exception e) {
            addError("customer.error.saving");
            log.debug(e);
            return "customerForm";
        }
        return "customer";
    }

    public String delete() {
        customerManager.remove(customer.getId());
        addMessage("customer.deleted", customer.getName());
        return "customer";
    }

    public String cancel() {
        return "customer";
    }

    /**
     * Convenience method to determine if the user came from the list screen
     * @return String
     */
    public String getFrom() {
        if ((getParameter("editUser:add") != null)
                || ("list".equals(getParameter("from")))) {
            return "list";
        }

        return "";
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the customerManager
     */
    public CustomerManager getCustomerManager() {
        return customerManager;
    }

    /**
     * @param customerManager the customerManager to set
     */
    public void setCustomerManager(CustomerManager customerManager) {
        this.customerManager = customerManager;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
}
